﻿using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

namespace WebResourcesManager.UserControls
{
    /// <summary>
    /// Controls that display an icon web resource
    /// </summary>
    public partial class IconControl : UserControl, IWebResourceControl
    {
        #region Variables

        /// <summary>
        /// Base64 content of the web resource when loading this control
        /// </summary>
        string originalContent;

        /// <summary>
        /// Base64 content of the web resource
        /// </summary>
        string innerContent;

        #endregion Variables

        #region Delegates

        public delegate void WebResourceUpdatedEventHandler(object sender, WebResourceUpdatedEventArgs e);

        #endregion

        #region Event Handlers

        public event WebResourceUpdatedEventHandler WebResourceUpdated;

        #endregion

        #region Constructor
        
        /// <summary>
        /// Initializes a new instance of class IconControl
        /// </summary>
        /// <param name="resource">Base64 content of the web resource</param>
        public IconControl(string content)
        {
            InitializeComponent();

            this.originalContent = content;
            this.innerContent = content;
        }

        #endregion Constructor

        #region Handlers

        private void Icon_Load(object sender, EventArgs e)
        {
            Icon ic = null;

            byte[] imageBytes = Convert.FromBase64String(this.innerContent);
            using (MemoryStream ms = new MemoryStream(imageBytes))
            {
                ic = new Icon(ms);
            }

            this.pictureBox1.Image = ic.ToBitmap();

            this.pictureBox1.Height = this.pictureBox1.Image.Size.Height;
            this.pictureBox1.Width = this.pictureBox1.Image.Size.Width;

            if (this.pictureBox1.Width > this.panel1.Width)
                this.pictureBox1.Width = this.panel1.Width;

            this.pictureBox1.Location = new Point(
                this.Width / 2 - this.pictureBox1.Width / 2,
                this.Height / 2 - this.pictureBox1.Height / 2);
        }

        #endregion Handlers

        #region Methods

        public string GetBase64WebResourceContent()
        {
            return this.innerContent;
        }

        public void ReplaceWithNewFile()
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Title = "Select a file to replace the existing web resource";
            ofd.Filter = "ICO file (*.ico)|*.ico";

            if (ofd.ShowDialog() == DialogResult.OK)
            {
                this.innerContent = Convert.ToBase64String(File.ReadAllBytes(ofd.FileName));
                this.Icon_Load(null, null);

                this.SendSavedMessage();
            }
        }

        private void SendSavedMessage()
        {
            WebResourceUpdatedEventArgs wrueArgs = new WebResourceUpdatedEventArgs()
            {
                Base64Content = this.innerContent,
                IsDirty = (this.innerContent != this.originalContent),
                Type = AppCode.Enumerations.WebResourceType.Ico
            };

            if (this.WebResourceUpdated != null)
            {
                this.WebResourceUpdated(this, wrueArgs);
            }
        }

        #endregion  Methods
    }
}
